package com.tr.syscms.utils;

import cn.hutool.core.date.DateUtil;
import com.tr.common.util.BusinessConstant;
import com.tr.common.util.JwtUtils;
import com.tr.syscms.entity.OperLog;
import com.tr.syscms.service.OperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ fileName:LogRecordUtil
 * @ description: 自定义切面类 用于aop的日志记录
 * @ author:wxh
 * @ createTime:2022/6/10 16:24
 * @ version:1.0.0
 */
@Component
@Aspect
public class LogRecordUtil {


    @Autowired
    private OperLogService operLogService;

    /**
     * 后置切面方法 切入点
     *
     * @param joinPoint
     */
    @AfterReturning(pointcut = "execution(* com.tr.syscms.service.impl.*.insert*(..)) ||"+
            "execution(* com.tr.syscms.service.impl.*.update*(..))||" +
            "execution(* com.tr.syscms.service.impl.*.delete*(..))")
    public void recordLog(JoinPoint joinPoint) {
        OperLog operLog = new OperLog();
        operLog.setTitle("系统模块");
        //获取方法名称 拿到方法全路径controller
        String name = joinPoint.getSignature().getDeclaringTypeName();    //方法全路径
//        System.out.println("方法全路径："+name);
//        System.out.println("方法全路径："+joinPoint.getSignature().getName());   获取方法名
        //设置业务类型（0其它 1新增 2修改 3删除）
        int bizType = BusinessConstant.LogRecord.OPERATION_INSERT;
        if (name.contains("update")) {
            bizType = BusinessConstant.LogRecord.OPERATION_UPDATE;
        } else if (name.contains("delete")) {
            bizType = BusinessConstant.LogRecord.OPERATION_DELETE;
        }
        operLog.setBusinessType(bizType);
        operLog.setMethod(name);
        //设置用户类型
        operLog.setOperatorType(BusinessConstant.LogRecord.USERTYPE_BACK);

        //设置操作人的部门(需要动态获取)

        //设置操作人的部门(需要动态获取)
        operLog.setDeptName("开发部");
        //请求的URL地址(HttpServletRequest对象的获取 可以百度)
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        StringBuffer requestURL = request.getRequestURL();
        operLog.setOperUrl(requestURL.toString());
        //拿到token 得到id  得到用户
        String token = request.getParameter("token");
        //System.out.println("AOP得到的token值"+token);
        String username = JwtUtils.getMessageIdByJwtToken(token).get("username");

        //设置操作人名称（需要动态获取）
        operLog.setOperName(username);

        //请求IP
        String remoteAddr = request.getRemoteAddr();
        operLog.setOperIp(remoteAddr);
        //设置操作地点
        operLog.setOperLocation("zz");
        //获取请求参数
        // 获取所有参数
        Object[] args = joinPoint.getArgs();
        String argName = null;
        if(args!=null&&args.length>0){
            argName = args[0].getClass().getName();
        }
        //设置方法请求参数  com.aaa.crms.entity.Car
        operLog.setOperParam(argName);
        //设置状态 0正常1异常
        operLog.setStatus(0);
        operLog.setErrorMsg(null);
        operLog.setOperTime(DateUtil.date());
        operLogService.save(operLog);

    }

}
